package com.herench.bs.handler;

import com.herench.bs.agent.IOutpatientClinicAgent;
import com.herench.bs.annotation.Handler;
import com.herench.bs.support.AbstractMessageHandler;
import com.herench.bs.dao.IClinicAppointmentRecordDao;
import com.herench.bs.support.RequestMessage;
import com.herench.bs.support.ResponseMessage;
import com.herench.bs.utils.DataFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.util.*;

/**
 * com.herench.bs.handler
 * Created by wenkai on 2017/09/12/13:53
 *
 * @Software: IntelliJ IDEA
 */
@Component
@Handler(code = "100901",name="挂号信息列表查询")
public class LoadingAllRegistHandler extends AbstractMessageHandler {

    @Autowired
    private IClinicAppointmentRecordDao clinicAppointmentRecordDao;
    @Override
    protected ResponseMessage innerHandler(RequestMessage requestMessage) throws Exception {
        ResponseMessage responseMessage = new ResponseMessage(requestMessage.getId());
        String channelCode = requestMessage.getArgsPropertyStringValue("sysCode");
        Assert.hasLength(channelCode,"sysCode 不可为空");
        String username = requestMessage.getArgsPropertyStringValue("user");
        String idCard = requestMessage.getArgsPropertyStringValue("idCard");
        Assert.hasLength(idCard,"idCard 不可为空");
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -3);
        String startTime = DataFormatUtils.getSimpleTimeString(calendar.getTime());
        calendar.add(Calendar.MONTH, 3);
        calendar.add(Calendar.DAY_OF_MONTH,20);
        String endTime = DataFormatUtils.getSimpleTimeString(calendar.getTime());
        IOutpatientClinicAgent outpatientClinicAgent = getCleverClinicContext().getApplicationContext().getBean(IOutpatientClinicAgent.class);
        Map<String, Object> params = new HashMap<>(16);
        String hospitalCode ="0001";
//        params.put("personId",idCard);
//        params.put("hospitalCode",hospitalCode);
//        params.put("channelCode","100103");
//        params.put("state","4");
//        List<Map<String, Object>> registResList = new ArrayList<>();
//        registResList = clinicAppointmentRecordDao.find(params);
//        if (registResList.size()>0){
//            String recordId = (String) registResList.get(0).get("id") ;
//            Map<String, Object> resPatientId = outpatientClinicAgent.queryPatientInfo(channelCode, hospitalCode, idCard);
//            String patientId = (String) resPatientId.get("patientId");
//            Map<String, Object> regist = new HashMap<>(16);
//            regist = outpatientClinicAgent.canceleRegisterTransactionInsurance(channelCode, hospitalCode, recordId, patientId);
//        }
        Map<String, Object> appoint = new HashMap<>(16);
        appoint.put("personId",idCard);
        appoint.put("hospitalCode",hospitalCode);
        appoint.put("channelCode","100103");
        appoint.put("state","5");
        List<Map<String, Object>> appointList = new ArrayList<>();
        appointList = clinicAppointmentRecordDao.find(appoint);
        if (appointList.size()>0){
            String recordId = (String) appointList.get(0).get("id") ;
            Map<String, Object> resPatientId = outpatientClinicAgent.queryPatientInfo(channelCode, hospitalCode, idCard);
            String patientId = (String) resPatientId.get("patientId");
            Map<String, Object> appointDao = new HashMap<>(16);
            appointDao = outpatientClinicAgent.canceleAppointTransactionInsurance(channelCode, hospitalCode, recordId, patientId);
        }
        Map<String, Object> res = outpatientClinicAgent.loadAllRegistRecords(channelCode, hospitalCode, startTime, endTime, idCard);

        responseMessage.setData(res);
        getPropertiesFromInvokerResultMap(responseMessage, res);
        return responseMessage;
    }
}
